<?php
/**
 * 游戏开放接口中间件
 * panglishan
 * 2020.10.13
 */

namespace App\Http\Middleware;

use App\Exceptions\BasicException;
use Closure;
use App\Services\Tools\JwtService;


class GameMiddleware {

    public function handle($request, Closure $next) {


        $token = $request->header('token','');
        $sign = $request->header('sign','');

        $params = $request->all();
        arsort($params);
        $param = http_build_query($params);
        $count_sign = md5($param.env('YUTANG_GAME_SALT'));

        if($sign != $count_sign) throw new BasicException(50008);

        if($request->path() != 'openapi/match/settlement')
        {
            $user_id = JwtService::getUid($token, true);

            if (!defined('UID')) {
                define('UID', $user_id);
            }
        }

        return $next($request);
    }

}
